package com.zpark.express.controller;

import com.zpark.express.constant.Constants;
import com.zpark.express.domain.Admin;
import com.zpark.express.result.Result;
import com.zpark.express.result.ReusltCode;
import com.zpark.express.service.AdminService;
import com.zpark.express.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Desc: 管理员登录
     * 1 先校验输入输入的信息
     * 2 根据用户名和密码查询管理员
     * 3 判断账号是否被禁用
     * 4 没有禁用，存token，保存管理员登录信息
     * @Date: 2020/7/18
     */
    @GetMapping("/login")
    public Result login(Admin admin) {
        try {
            //校验传过来的数据是否为空
            if (admin.getAdminName() == null || admin.getAdminName().equals("")
                    || admin.getAdminPwd() == null || admin.getAdminPwd().equals("")) {
                return Result.customize(ReusltCode.FAIL, Constants.INPUT_INFO_NOTNULL);
            }
            //查询管理员
            Admin admin1 = adminService.findByNameAndPwd(admin);
            if (admin1 != null && !admin1.equals("")) {
                if (admin1.getAdminState() != 1) {
                    //1是启用2是禁用
                    return Result.customize(ReusltCode.FAIL, Constants.ACCOUNT_DISABLE);
                }
                //登录成功将用户信息存入redis中，还产生token
                String token = JwtUtil.createToken(admin1);
                redisTemplate.opsForValue().set("admin-token",token);
                redisTemplate.expire("admin-token",1, TimeUnit.DAYS);
                redisTemplate.opsForHash().put("login","admin",admin1);
                return Result.addResultOk(admin1, Constants.LOGIN_SUCCESS);
            }
            //查询失败
            return Result.customize(ReusltCode.FAIL, Constants.LOGIN_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }


    }

    /**
     * @Desc: 测试能否从redis中获取token,并解析jwt,从而获取用户信息
     * @Date: 2020/7/18
     */
    @GetMapping("/test")
    public Admin test(){
        Object o = redisTemplate.opsForValue().get("admin-token");
        Claims claim = JwtUtil.getClaim((String) o);
        System.out.println("claims="+claim);
        System.out.println(JwtUtil.getAdmin((String)o));
        return JwtUtil.getAdmin((String)o);
    }


    /**
     * @Desc: 查询所有管理员
     * @Date: 2020/7/18
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            Result result= adminService.findAll();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new  RuntimeException();
        }
    }


    /**
     * @Desc: 修改管理员信息
     * @Date: 2020/7/18
     */
    @PutMapping("/updateById")
    public Result updateById(Admin admin){
        Result result=adminService.updateByAdminId(admin);
        return result;
    }


    /**
    * @Desc: 根据id删除管理员信息
    * @Date: 2020/7/18
    */

    @DeleteMapping("/deleteById")
    public Result deleteById(Admin admin){
        Result result=adminService.deleteById(admin.getAdminId());
        return result;
    }
}
